<?php

require_once './application/core/CoreBase.php';
require_once './library/XML/xmlToObjArray.php';
require_once './application/models/Objets/Composant.php';
require_once './application/models/Tables/cache.php';
require_once './application/entity/creation/creation.php';
require_once './application/entity/creation/map.php';
require_once './application/core/Client/creationSoap.php';
require_once './application/core/Detail/detailCore.php';

class creationCore extends CoreBase
{

    private $_name;
    private $_data;
    private $_obj;

    public function createMap()
    {
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $check = $this->checkPresent($this->_name);
        $obj = new checkNameMap();
        $obj->error = $check;
        echo Zend_Json_Encoder::encode($obj);
        if ($check == true)
            return false;
        return true;
    }

//check si nom de map deja present dans la base
    private function checkPresent()
    {
        $table = new TableMap();
        $check = $table->getMap($this->_name);
        if ($check == false)
            return;
        return true;
    }

//Parse le cache pour avoir juste le xml
    private function parseLinkMapCache($xml)
    {
        $obj = new xmlToObjArray();
        $xml = $obj->parse_xml_data($xml, false);
        $xml = $xml[0]['PARAM'][0];
        return $xml;
    }

//parse les donne du tableau et du cache
    private function parseArrayMap()
    {
        $line = null;
        $this->_data = explode(",", $this->_data);
        foreach ($this->_data as $val)
        {
            $val = str_replace("[{", "", $val);
            $val = str_replace("}]", "", $val);
            $line .= $val . ";";
        }
        return $line;
    }

// fait la requete a la sonde
    public function liensIp()
    {
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $this->_data = $this->_controller->getRequest()->getParam('data');
        $this->_data = $this->parseArrayMap();
        $params = array('ListeIp' => $this->_data,
            'map' => $this->_name);
        $c = new creationSoap($this->_controller);
        $c->liensIp($params, 'liensipresp');
        $this->MiseEnAttenteReponse(true, '/creation/liensipresp', null);
    }

//reponse pour les liens
    public function liensIpResp()
    {
        $cache = new cache();
        $rst = $cache->getInfo($this->_controller->_idClient, "liensipresp");
        if ($rst != false)
        {
            $cache->deleteInfo($this->_controller->_idClient, 'liensipresp', $rst[0]['Id']);

            $xml = $this->parseLinkMapCache($rst[0]['Xml']);
            $this->_name = $xml['MAP'][0]['NAME'][0][0];
            $this->detailListIp($xml['RETURN'][0]['LISTEIP'][0][0], $this->_name);
            if (isset($xml['LIENS'][0]['DATA'][0][0]))
            {
                $this->_data = $xml['LIENS'][0]['DATA'][0][0];
                $this->_data = $this->parseArrayMap();
            }
            else
                $this->_data = false;
            $this->saveCreateMap();
            $this->_obj = new dataMap();
            $this->_obj->name = $this->_name;
            $this->MiseEnAttenteReponse(false, '/creation/liensipresp', $this->_obj);
        }
        else
            $this->MiseEnAttenteReponse(true, '/creation/liensipresp', null);
    }

    public function liensIpRespList($idClient)
    {
        $cache = new cache();
        $rst = $cache->getInfo($idClient, "liensipresp");
        if ($rst != false)
        {
            foreach ($rst as $key => $val)
            {
                $cache->deleteInfo($idClient, 'liensipresp', $rst[$key]['Id']);
                $xml = $this->parseLinkMapCache($rst[$key]['Xml']);
                $this->_name = $xml['MAP'][0]['NAME'][0][0];
                if (isset($xml['LIENS'][0]['DATA'][0][0]))
                {
                    $this->_data = $xml['LIENS'][0]['DATA'][0][0];
                    $this->_data = $this->parseArrayMap();
                }
                else
                    $this->_data = false;
                $this->saveCreateMap();
            }
        }
        return false;
    }

    private function detailListIp($list, $name)
    {
        $obj = new detailCore($this->_controller);
        $list = explode(';', $list);
        foreach ($list as $ip)
        {
            sleep(1);
            $obj->detailIp($ip, $name);
        }
    }

    private function saveCreateMap()
    {
        $table = new ObjectComposant();
        $table->ajouterMap($this->_name, $this->_data);
        return true;
    }

    public function deleteMap()
    {
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $table = new ObjectComposant();
        $table->supprimeMap($this->_name);
    }

}
?>
